package cn.wit.tech.service.impl;


import cn.wit.tech.condition.SearchProjectExerciseCondition;
import cn.wit.tech.mapper.ProjectExerciseMapper;
import cn.wit.tech.mapper.TechDirectionMapper;
import cn.wit.tech.mapper.TechPointMapper;
import cn.wit.utils.pojo.ProjectExercise;
import cn.wit.utils.pojo.TechDirection;
import cn.wit.utils.pojo.TechPoint;
import cn.wit.tech.service.ProjectExerciseService;
import cn.wit.tech.vo.ProjectExerciseVO;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectExerciseServiceImpl implements ProjectExerciseService {
    @Autowired
    private ProjectExerciseMapper projectExerciseMapper;
    @Autowired
    private TechDirectionMapper techDirectionMapper;
    @Autowired
    private TechPointMapper techPointMapper;


    @Override
    public Result insert(ProjectExercise projectexercise) {
        int i = projectExerciseMapper.insert(projectexercise);
        if(i > 0){
            return Result.ok();
        }
        return Result.error("新增失败");
    }

    @Override
    public Result deleteById(int id) {
        int i = projectExerciseMapper.deleteById(id);
        if(i > 0){
            return Result.ok();
        }
        return Result.error("删除失败");
    }

    @Override
    public Result updateById(ProjectExercise projectexercise) {
        int i = projectExerciseMapper.updateById(projectexercise);
        if(i > 0){
            return Result.ok();
        }
        return Result.error("更新失败");
    }


    @Override
    public Result searchPage(IPage<ProjectExercise> page, SearchProjectExerciseCondition condition) {
        QueryWrapper<ProjectExercise> wrapper3 = new QueryWrapper<>();
        if(condition != null) {
            if(condition.getDirectionName() != null && condition.getDirectionName() != "") {
                QueryWrapper<TechDirection> wrapper = new QueryWrapper<>();
                wrapper.eq("direction_Name", condition.getDirectionName());
                TechDirection techDirection = techDirectionMapper.selectOne(wrapper);
                if(techDirection != null) {
                    wrapper3.eq("techdirection_ID", techDirection.getTechDirectionId());
                }else {
                    wrapper.eq("techdirection_ID", 0);
                }
            }
            if(condition.getProjectName() != null && condition.getProjectName() != "") {
                wrapper3.like("project_Name", condition.getProjectName());
            }
            if(condition.getTechPointName() != null && condition.getTechPointName() != "") {
                QueryWrapper<TechPoint> wrapper = new QueryWrapper<>();
                wrapper.eq("techpoint_Name", condition.getTechPointName());
                TechPoint techPoint = techPointMapper.selectOne(wrapper);
                if(techPoint != null) {
                    wrapper3.eq("techpoint_ID", techPoint.getTechPointId());
                } else{
                    wrapper3.eq("techpoint_ID", 0);
                }
            }
        }
        IPage<ProjectExercise> projectExerciseIPage = projectExerciseMapper.selectPage(page, wrapper3);

        List<ProjectExerciseVO> list = new ArrayList<>();
        for(ProjectExercise pe : projectExerciseIPage.getRecords()) {
            ProjectExerciseVO vo = new ProjectExerciseVO();
            TechDirection techDirection = techDirectionMapper.selectById(pe.getTechDirectionId());
            TechPoint techPoint = techPointMapper.selectById(pe.getTechPointId());
            BeanUtils.copyProperties(pe, vo);
            vo.setTechDirection(techDirection.getDirectionName());
            vo.setTechPoint(techPoint.getTechPointName());
            list.add(vo);
        }

        Page<ProjectExerciseVO> voPage = new Page<>(projectExerciseIPage.getCurrent(), projectExerciseIPage.getSize(), projectExerciseIPage.getTotal(), true);
        voPage.setRecords(list);
        return Result.ok(voPage);
    }

}
